package com.debugassistant.service.crawl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.neowebcrawler.entity.WebContent;
import org.neowebcrawler.httpclient.HttpClientAgent;
import org.neowebcrawler.parse.IParser;

import com.debugassistant.dao.ESTRepository;
import com.debugassistant.entity.EST;
import com.debugassistant.service.est.ESTQueryGenerator;

public class MyGoogleExceptionInfoParser implements IParser {

	public static final String PATH_ROOT = "E:\\ExceptionGoogleJob\\";
	
	public static final String TYPE_Google_RESULT_PAGE = "GOOGLE_RESULT_PAGE";
	
	public static final String TYPE_FIRST_LAYER_PAGE = "GOOGLE_FIRST_LAYER_PAGE";
	
	@Override
	public List<WebContent> getAllSeeds() {
		List<WebContent> webContents = new ArrayList<WebContent>();

		try {
			List<EST> ests = ESTRepository.getDistinctAll();
			for(EST est : ests)
			{
				List<String> queries = ESTQueryGenerator.generateGoogleQueries(est);
				for (String query : queries) {
					WebContent webContent = new WebContent();
					webContent.setType(TYPE_Google_RESULT_PAGE);
					webContent.setUrl(query);
					webContents.add(webContent);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return webContents;
	}

	@Override
	public String getRootPath() {
		return PATH_ROOT;
	}

	@Override
	public List<WebContent> parse(WebContent webContent, String webPage) {
		List<WebContent> webContentList = new ArrayList<WebContent>();
		if(webContent.getType() == TYPE_Google_RESULT_PAGE) {
			if(webPage != null) {
				Parser parser = new Parser();
				try {
					parser.setInputHTML(webPage);
					NodeFilter filter = new HasAttributeFilter("class", "r");
					NodeList list = parser.extractAllNodesThatMatch(filter);
					for(int i = 0; i < list.size(); i++) {
						String anchor = list.elementAt(i).toHtml();
						int beginIndex, endIndex;
						beginIndex = anchor.indexOf("a href=\"") + 8;
						endIndex = anchor.indexOf("\"", beginIndex);
						anchor = anchor.substring(beginIndex, endIndex);
						System.out.println(anchor);
						WebContent webContent2 = new WebContent();
						webContent2.setType(TYPE_FIRST_LAYER_PAGE);
						webContent2.setUrl(anchor);
						webContentList.add(webContent2);
					}
				} catch (ParserException e) {
					e.printStackTrace();
				}
			}
		}
		
		if(webContent.getType() == TYPE_FIRST_LAYER_PAGE) {
			
		}
		return webContentList;
	}
	
	public static void main(String[] args) {
		MyGoogleExceptionInfoParser parser = new MyGoogleExceptionInfoParser();
		WebContent webContent = new WebContent();
		webContent.setType(TYPE_Google_RESULT_PAGE);
		webContent.setUrl("http://www.google.com/search?q=java+exception+null+pointer&hl=en");
		String webPage = HttpClientAgent.getMethod(webContent);
		parser.parse(webContent, webPage);
	}
}